<%@ include file="/common/taglibs.jsp"%>

<head>
<title><fmt:message key="userProfile.title" /></title>
<meta name="menu" content="UserMenu" />
</head>

<c:set var="delObject" scope="request">
	<fmt:message key="userList.user" />
</c:set>
<script type="text/javascript">
	var msgDelConfirm = "<fmt:message key="delete.confirm"><fmt:param value="${delObject}"/></fmt:message>";
</script>

<%-- <div class="span2">
	<h2>
		<fmt:message key="userProfile.heading" />
	</h2>
	<c:choose>
		<c:when test="${param.from == 'list'}">
			<p>
				<fmt:message key="userProfile.admin.message" />
			</p>
		</c:when>
		<c:otherwise>
			<p>
				<fmt:message key="userProfile.message" />
			</p>
		</c:otherwise>
	</c:choose>
</div> --%>
<div class="span7">
	<security:authentication property="principal.id" var="curUserId"/>
	<s:set value="false" name="readonly"/>
	<security:authorize ifAllGranted="ROLE_ADMIN">
		<c:if test="${param.id != null and curUserId != param.id}">
			<s:set value="true" name="readonly"/>
		</c:if>
	</security:authorize>

	<s:form name="userForm" action="saveUser" method="post" validate="true"
		cssClass="well form-horizontal" autocomplete="off" theme="bootstrap"
		label="%{getText('userProfile.heading')}">
		<s:hidden key="user.id" />
		<s:hidden key="user.version" />
		<input type="hidden" name="from" value="${param.from}" />

		<c:if test="${cookieLogin == 'true'}">
			<s:hidden key="user.password" />
			<s:hidden key="user.confirmPassword" />
		</c:if>
		<s:if test="user.version == null">
			<input type="hidden" name="encryptPass" value="true" />
		</s:if>

		<s:textfield key="user.username" required="true" readonly="true"/>

		<c:if test="${cookieLogin != 'true'}">
			<s:password key="user.password" showPassword="true" required="true"
				onchange="passwordChanged(this)" />

			<s:password key="user.confirmPassword" required="true"
				showPassword="true" onchange="passwordChanged(this)" />
		</c:if>
		
		<s:textfield key="user.companyName" required="true" readonly="%{#readonly}"/>

		<crs:idtype key="user.idtype" required="true" listKey="value"
			listValue="label"/>
		<s:textfield key="user.idnumber" required="true"  readonly="%{#readonly}"/>
		<s:textfield key="user.contact" required="true"  readonly="%{#readonly}"/>
		<s:textfield key="user.contactNumber" required="true"  readonly="%{#readonly}"/>
		<s:textfield key="user.phoneNumber" required="true" readonly="%{#readonly}"/>
		<s:textfield key="user.email" required="true" readonly="%{#readonly}"/>
		<s:textfield key="user.fax" readonly="%{#readonly}"/>
		<s:textfield key="user.address.address" required="true" readonly="%{#readonly}"/>
		<s:textfield key="user.address.postalCode" readonly="%{#readonly}"/>

		<c:choose>
			<c:when test="${param.from == 'list'}">
				<fieldset class="control-group">
					<label class="control-label"><fmt:message
							key="userProfile.accountSettings" /></label>
					<div class="controls">

						<label class="radio inline"><s:radio name="user.enabled"
								value="%{user.enabled}" id="user.enabled" list="userStatusList"
								listKey="value" listValue="label" labelposition="inline"
								theme="simple" /> </label>
					</div>


				</fieldset>

				<fieldset class="control-group">
					<label for="userRoles" class="control-label"><fmt:message
							key="userProfile.assignRoles" /></label>
					<div class="controls">
						<select id="userRoles" name="userRoles" multiple="true">
							<c:forEach items="${availableRoles}" var="role">
								<option value="${role.value}"
									${fn:contains(user.roles, role.label) ? 'selected' : ''}>${role.label}</option>
							</c:forEach>
						</select>
					</div>
				</fieldset>
			</c:when>
			<c:otherwise>
				<fieldset class="control-group">
					<label class="control-label"><fmt:message key="user.roles" />:</label>
					<div class="controls readonly">
						<s:iterator value="user.roleList" status="status">
							<s:property value="label" />
							<s:if test="!#status.last">,</s:if>
							<input type="hidden" name="userRoles"
								value="<s:property value="value"/>" />
						</s:iterator>
					</div>
					<s:hidden name="user.enabled" value="%{user.enabled}" />
				</fieldset>
			</c:otherwise>
		</c:choose>
		<div id="actions" class="form-actions">
			<s:submit type="button" cssClass="btn btn-primary" method="save"
				key="button.save" theme="simple">
				<i class="icon-ok icon-white"></i>
				<fmt:message key="button.save" />
			</s:submit>
			<c:if test="${param.from == 'list' and not empty user.id}">
				<s:submit type="button" cssClass="btn btn-danger" method="delete"
					key="button.delete" onclick="return confirmMessage(msgDelConfirm)"
					theme="simple">
					<i class="icon-trash"></i>
					<fmt:message key="button.delete" />
				</s:submit>
			</c:if>
			<s:submit type="button" cssClass="btn" method="cancel"
				key="button.cancel" theme="simple">
				<i class="icon-remove"></i>
				<fmt:message key="button.cancel" />
			</s:submit>
		</div>
	</s:form>
</div>

<c:set var="scripts" scope="request">
	<script type="text/javascript">
		function passwordChanged(passwordField) {
			if (passwordField.name == "user.password") {
				var origPassword = "<s:property value="user.password"/>";
			} else if (passwordField.name == "user.confirmPassword") {
				var origPassword = "<s:property value="user.confirmPassword"/>";
			}

			if (passwordField.value != origPassword) {
				createFormElement("input", "hidden", "encryptPass",
						"encryptPass", "true", passwordField.form);
			}
		}
	</script>
</c:set>
<script type="text/javascript">
	$(document).ready(
			function() {
				$("input[type='text']:visible:enabled:first",
						document.forms['userForm']).focus();
				
				$("#messagesContainer").hide();
				<security:authorize ifAllGranted="ROLE_ADMIN">
					<c:if test="${param.id != null and curUserId != param.id}">
						$(":input[name='user.idtype']").attr("readonly",true); 
						$(":input[name='user.idtype'] option:not(:selected)").attr('disabled', true);
					</c:if>
				</security:authorize>

			});
</script>
